package com.guc.gviewktx

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.guc.gview.ScaleView
import com.guc.gview.loading.CoffeeDrawable
import com.guc.gview.loading.LineLoadingDrawable
import com.guc.gview.textview.ExpandableTextView
import com.guc.gviewbinding.inflate
import com.guc.gviewktx.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private val binding:ActivityMainBinding by inflate()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        testRiseNumber()
        testScaleView()
        testPanelView()
        testExpandTextView()
        testLineLoading()
        testSignView()
    }

    private fun testRiseNumber() {
        binding.riseNumber.withNumber(9999999).start()
    }

    private fun testSignView() {
        binding.btnSaveSign.setOnClickListener {
            if (binding.signNameView.hasSign()) {
                val success =
                        binding.signNameView.saveSign("$externalCacheDir/${System.currentTimeMillis()}.jpeg")
                if (success) {
                    Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show()
                }
            } else {
                Toast.makeText(this, "未签名", Toast.LENGTH_LONG).show()
            }
        }
        binding.btnClearSign.setOnClickListener {
            binding.signNameView.clean()
        }
    }

    private fun testLineLoading() {
        val lineLoading = LineLoadingDrawable()
        binding.ivLineLoading.setImageDrawable(lineLoading)
        binding.ivLineLoading.post {
            lineLoading.start()
        }
        val coffeeDrawable = CoffeeDrawable.create(binding.ivCoffeeLoading)
        coffeeDrawable.progress = 1f
        binding.ivCoffeeLoading.postDelayed({
            coffeeDrawable.start()
        }, 1000)
    }

    private fun testExpandTextView() {
        binding.etv.currentState = ExpandableTextView.STATE_SHRINK
    }

    private fun testPanelView() {
        binding.panelView.percent = 20
        binding.panelView2.setValue(50f)
        binding.panelView2.needBulge = true
    }

    private fun testScaleView() {
        binding.scaleView.type = ScaleView.SCALE_TYPE_TEMPERATURE
        binding.scaleView.minValue = 35
        binding.scaleView.maxValue = 42
        binding.scaleView.value = 36.5f
    }
}